<template>
  <van-form @submit="onSubmit">
    <van-cell-group inset>
      <van-field
          v-model="editUser.currentValue"
          :name="editUser.editKey"
          :label="editUser.editName"
          :placeholder="`请输入${editUser.editName}`"
      />
    </van-cell-group>
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        提交
      </van-button>
    </div>
  </van-form>

</template>

<script setup lang="ts">
import {useRoute, useRouter} from "vue-router";
import {onMounted, ref} from "vue";
import myAxios from "../plugins/myAxios"
import {getCurrentUser} from "../services/user.ts";
import {Toast} from "vant";
import {getCurrentUserState} from "../states/user.ts";
import {c} from "vite/dist/node/types.d-aGj9QkWt";
const route = useRoute();
const router = useRouter();
const editUser = ref({
  editKey: route.query.editKey,
  editName: route.query.editName,
  currentValue: route.query.currentValue,
})



//todo 把editKey、editName、currentValue提交到后端
const onSubmit = async () => {
  const currentUser = await getCurrentUser()
  if(!currentUser){
    Toast.fail("用户未登录")
    return;
  }
  const res = await myAxios.post('/user/update', {
    'id': currentUser.id,
    [editUser.value.editKey]: editUser.value.currentValue,
  })
  if(res.code === 1 && res.data > 0){
    Toast.success("修改成功")
    router.back();
  }else{
    Toast.fail("修改失败")
  }

};
</script>
<style scoped>

</style>